package com.wenjie.shoprankmonitor;

import java.util.List;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long user_id;
	
	@Persistent
	private String nickname;
	
	@Persistent
	private String password;
	
	@Persistent(mappedBy = "user")
	private List<Key_monitor_list> monitor_keys;
	
	public User(String nickname, String password) {
		this.nickname = nickname;
		this.password = password;
	}
	
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	
	public Long getUser_id() {
		return user_id;
	}
	
	public String getNickname() {
		return nickname;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public List<Key_monitor_list> getMonitor_keys() {
		return monitor_keys;
	}
	
	public void addKey(Long key_id, String key_name) {
		monitor_keys.add(new Key_monitor_list(key_id, key_name));
	}
	
	public void delKey(int index) {
		if(index < monitor_keys.size()) {			
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Key_monitor_list key = pm.getObjectById(Key_monitor_list.class, monitor_keys.get(index).getKey());
			pm.deletePersistent(key);
			pm.close();
		}
	}
}
		